package classes
{
	import classes.event.QueryEvent;
	import classes.event.SessionEvent;
	import classes.tfo.SkillTO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	
	import mx.collections.XMLListCollection;
	
	public class SkillLoader extends EventDispatcher
	{
		private static var instance:SkillLoader = new SkillLoader();
		
		private var loader:URLLoader;
		
		private var cache:Array = [];
		
		public function SkillLoader()
		{
			loader = new URLLoader();
			configureListeners(loader);
			Red5Facade.getInstance().addEventListener(SessionEvent.UNSET, handleSessionUnset);
		}
		
		private function handleSessionUnset(event:SessionEvent):void {
			cache = [];
		}
		
		public function loadSkillForClass(classId:int):void {
			if(cache.length > 0) {
				dispatchSkill();
			} else {
				var req:URLRequest = new URLRequest("skills/skill" + classId + ".xml");
				var header:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
				req.requestHeaders.push(header);
				loader.load(req);
			}
		}
	
		private function configureListeners(dispatcher:IEventDispatcher):void {
      dispatcher.addEventListener(Event.COMPLETE, completeHandler);
	    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
	    dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
	    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    }

    private function completeHandler(event:Event):void {
    	cache = [];
      var ld:URLLoader = URLLoader(event.target);
      var xmlList:XMLListCollection = new XMLListCollection();
			var raw:XML = new XML(ld.data);
			xmlList.source = raw.skillEntry;
			for(var i:int = 0; i < xmlList.length; i++) {
				var xmlElement:Object = xmlList.getItemAt(i);
				cache.push(SkillTO.createInstance(xmlElement));
			}
      dispatchSkill();
    }
    
    private function dispatchSkill():void {
    	var ret:Array = [];
    	for(var i:int = 0; i < cache.length; i++) {
    		var skill:SkillTO = cache[i];
    		ret.push(skill.clone());
    	}
    	dispatchEvent(new QueryEvent(QueryEvent.LOAD_SKILL, ret));
    }
    
    public function getSkill(skillDbName:String):SkillTO {
    	for(var i:int = 0; i < cache.length; i++) {
    		var skill:SkillTO = cache[i];
    		if(skill.skillDbName == skillDbName) return skill.clone();
    	}
    	return null;
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
      trace("securityErrorHandler: " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
      trace("httpStatusHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
      trace("ioErrorHandler: " + event);
    }
	
		public static function getInstance():SkillLoader {
			return instance;
		}

	}
}